package com.etheart.lyric.htmlparser.service.jujing;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.springframework.stereotype.Service;

import com.etheart.framework.core.util.RegexUtils;
import com.etheart.model.entity.Singer;

@Service("htmlparse_JujingSingerParseService")
public class JujingSingerParseService {
	private static final Log logger = LogFactory
			.getLog(JujingSingerParseService.class);
//info-a8r3dcce.shtml
	public Singer getSingerFromParser(Parser parser,String fileName)
			throws IOException {
		String idStr = RegexUtils.getMatcheGroup(".*info-([^\\.]*)\\.shtml",fileName);
		Singer singer = new Singer();
	//	singer.setFromUrl(idStr);//-------------------------------
		return getSinger(parser, singer);
	}
	
	private Singer getSinger(Parser parser, Singer singer) {
		// 得到歌手名称
		try {
			String[] singerName = parseSingerName(parser);
			singer.setSingername(singerName[0]);
			singer.setByname(singerName[1]);
		} catch (ParserException e) {
			logger.error("qqsingerid:" + singer.getNetId()
					+ " singername:解析异常", e);
			e.printStackTrace();
		}
		
		//歌手图片
		try {
			String imgPath = parseSingerImg(parser);
			//singer.setPicUrl(imgPath);//----------------------
			if (StringUtils.isNotBlank(imgPath)&&imgPath.indexOf("SingerImg/nopic")==-1) {
				//没有图片
				String intId = RegexUtils.getMatcheGroup(".*singer([^\\.]*).jpg", imgPath);
				singer.setNetId(intId);
			}
		} catch (ParserException e) {
			logger.error("qqsingerid:" + singer.getNetId() + " picUrl:解析异常",
					e);
			e.printStackTrace();
		}
		
		// 歌手简介
		try {
			String intr = parseSingerIntro(parser);
			singer.setSingerintro(intr);
		} catch (ParserException e) {
			logger.error("qqsingerid:" + singer.getNetId()
					+ " singerintro:解析异常", e);
			e.printStackTrace();
		}
		
		
		
		// 歌手排行  靠 没有
	//	int rank = parseSingerRank(parser);

		return singer;
	}





	private String parseSingerIntro(Parser parser) throws ParserException {
		parser.reset();
		NodeFilter filter = new AndFilter(new TagNameFilter("div"),
				new HasAttributeFilter("class", "listen_singerintro_infroB"));
		NodeList nodeList = parser.parse(filter);
		if ( nodeList.size()<=0) {
			return null;
		}
		Tag tag = (Tag) nodeList.elementAt(0);
		return tag.toPlainTextString().trim();
	}

	private String parseSingerImg(Parser parser) throws ParserException {
		parser.reset();
		NodeFilter imgFilter = new AndFilter( new TagNameFilter("img"),new HasAttributeFilter("height","255"));
		// 得到歌手图片地址
		NodeList nodeList = parser.parse(imgFilter);
			Tag tag = (Tag) nodeList.elementAt(0);
			String src = tag.getAttribute("src");
			return src.replace("http://pic.top100.cn/", "");
		
	}

	// 解析歌手名
	private String[] parseSingerName(Parser parser) throws ParserException {
		parser.reset();
//		<div class="listen_singerintro_name">
//        <p><strong>Peter Buffett</strong></p>
//        <p>Peter Buffett</p>
//    </div>
		NodeFilter filter = new AndFilter(new TagNameFilter("div"),new HasAttributeFilter("class","listen_singerintro_name"));  
		NodeList nodeList = parser.parse(filter);
		
		String divHtml = nodeList.elementAt(0).toHtml().replaceAll("\n", "");
		String[] names = RegexUtils.getMatcheGroups(".*<strong>([^<]*)</strong></p>[^<]*<p>([^<]*)</p>.*", divHtml,2);
		return names;
	}
	
	public String getAlbumListUrl(Singer singer){
		return "/album/list.php?singerid="+singer.getNetId();
	}
	
	public String getAlbumListFileName(Singer singer){
		return "listsingerid="+singer.getNetId()+".php";
	}
}
